package com.yunhe.policyfulfillment.policyful.service.impl;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.common.util.DateTimeUtils;
import com.yunhe.policyfulfillment.constant.PolicyFulfillmentConstant;
import com.yunhe.policyfulfillment.policyful.domain.PolicyInfo;
import com.yunhe.policyfulfillment.policyful.model.PolicyInfoQueryCondition;
import com.yunhe.policyfulfillment.policyful.repository.PolicyInfoRepository;
import com.yunhe.policyfulfillment.policyful.service.PolicyInfoService;
import com.yunhe.policyfulfillment.util.MyHeaderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class PolicyInfoServiceImpl implements PolicyInfoService {

	@Autowired
    private PolicyInfoRepository policyInfoRepository;

	@Autowired
    private MyHeaderUtil myHeaderUtil;

    @Override
    public void deleteByIds(String ids) {
        String[] idsList = ids.split(",");
        for (String id : idsList) {
            policyInfoRepository.deleteByPrimaryKey(Long.valueOf(id));
        }
    }

    @Override
    public int insert(PolicyInfo record) throws ParseException {
        record.setPublisherId(myHeaderUtil.getHeaderUserId());
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date releaseDate = df.parse(record.getReleaseTime());
        long time = releaseDate.getTime();
        if(time <= System.currentTimeMillis()){
            record.setStatus(PolicyFulfillmentConstant.Online_Type.Online.value());
        }else {
            record.setStatus(PolicyFulfillmentConstant.Online_Type.Offline.value());
        }
        return policyInfoRepository.insert(record);
    }

    @Override
    public PolicyInfo selectByPrimaryKey(Long id) {
        return policyInfoRepository.selectByPrimaryKey(id);
    }

    @Override
    public void updateByPrimaryKeySelective(PolicyInfo record) throws ParseException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date releaseDate = df.parse(record.getReleaseTime());
        long time = releaseDate.getTime();
        if(time <= System.currentTimeMillis()){
            record.setStatus(PolicyFulfillmentConstant.Online_Type.Online.value());
        }else {
            record.setStatus(PolicyFulfillmentConstant.Online_Type.Offline.value());
        }
        policyInfoRepository.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<PolicyInfo> selectByCondition(PolicyInfoQueryCondition condition) {
        List<PolicyInfo> policyInfoList = policyInfoRepository.selectByCondition(condition);
        policyInfoList.forEach(policyInfo -> policyInfo.setPublisher(myHeaderUtil.getUserName(policyInfo.getPublisherId())));
        return policyInfoList;
    }

    @Override
    public List<PolicyInfo> selectEightData(String title) {
        return policyInfoRepository.selectEightData(title);
    }

    @Override
    public PageResult selectByConditionForPage(PolicyInfoQueryCondition condition) {
        PageMethod.startPage(condition.getPage(), condition.getSize());
        List<PolicyInfo> policyInfoList = selectByCondition(condition);
        policyInfoList.forEach(policyInfo -> policyInfo.setReleaseTime(DateTimeUtils.getDateStr(policyInfo.getReleaseTime(), "yyyy-MM-dd")));
        PageInfo<PolicyInfo> pageInfo = new PageInfo<>(policyInfoList);
        return new PageResult(condition.getPage(),condition.getSize(), (int) pageInfo.getTotal(), policyInfoList);
    }

    @Override
    public void onlineOrOffline(PolicyInfo record) {
        policyInfoRepository.updateByPrimaryKeySelective(record);
    }

    @Override
    public void patchOffline(String ids) {
        String[] idsList = ids.split(",");
        for (String id : idsList) {
            PolicyInfo policyInfo = policyInfoRepository.selectByPrimaryKey(Long.valueOf(id));
            policyInfo.setStatus(PolicyFulfillmentConstant.Online_Type.Offline.value());
            policyInfoRepository.updateByPrimaryKeySelective(policyInfo);
        }
    }

    @Override
    public void publishTimedPolicyInfo() {
        policyInfoRepository.publishTimedPolicyInfo();
    }
}